<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>

<form class="layui-form" id="pageSubmit" method="post">
<#-- 上半部分 - 条件查询 -->
    <fieldset class="layui-elem-field">
        <legend>买赠信息查询</legend>
    <#-- 上半部分内容区域 -->
        <div class="layui-field-box" >

            <#--买赠编号 隐藏 从数据库中查询-->
                <input type="hidden" name="id" value="${params.id!''}">

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">买赠名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="gName" value="${params.gName!''}" lay-verify="require" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">满赠促销方式</label>
                    <div class="layui-input-inline">
                        <select lay-filter="conNo" id="conNo" name="conNo">
                            <option value="">请选择赠送方式</option>
                        <#if conList ? exists>
                            <#list conList as item>
                                <option value="${item.conNo!''}">${item.conName!''}</option>
                            </#list>
                        <#else >
                        </#if>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">商品类别</label>
                        <div class="layui-input-inline">
                            <select id="pPrdNo" class="pPrdNo" lay-filter="pPrdNo" name="pPrdNo">
                                <option value="">请选择商品</option>
                            <#list map as info>
                                <option value="${info.prdNo!''}" abc="${info.prdName!''}">${info.prdName!''}</option>
                            </#list>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">赠品类别</label>
                        <div class="layui-input-inline">
                            <select id="gPrdNo" name="gPrdNo">
                                <option value="">请选择赠品</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="manageSLGF">确认</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
        </div>

    </fieldset>


<#-- 下半部分 - 查询结果 -->
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>买赠信息</legend>
    </fieldset>

    <div class="layui-form">
        <table class="layui-table">
            <colgroup>
                <col width="200">
                <col width="150">
                <col width="120">
                <col width="150">
                <col width="100">
                <col width="80">
            </colgroup>
            <thead>
            <tr>
                <th>买赠编号</th>
                <th>买赠名称</th>
                <th>买赠促销方式</th>
                <th>主品类别</th>
                <th>赠品类别</th>
                <th>管理</th>
            </tr>
            </thead>
            <tbody>
            <#list list as it>
            <tr>
            <#--买赠编号-->
                <td>${it.gNo!''}</td>
            <#--买赠名称-->
                <td>${it.gName!''}</td>
            <#--买赠促销方式-->
                <td>${it.conName!''}</td>
            <#--主品类别-->
                <#--<td>${it.prdName!''}</td>-->
                <td>${it.pPrdNo!''}</td>
            <#--赠品类别-->
                <td>${it.gPrdNo!''}</td>
            <#--详情按钮-->
                <td>
                    <div class="layui-btn-group">
                        <button type="button" value="${it.id!''}" class="layui-btn layui-btn-small detail">
                            <i class="layui-icon">管理</i>
                        </button>
                    </div>
                </td>
            </tr>
            </#list>
            </tbody>
        </table>
    </div>

    <div class="layui-form">
        <span id="form_page"></span>&nbsp;共${page.total}条数据
    </div>

    <input type="hidden" id="currentPage" name="currentPage">

</form>

<script type="text/javascript" src="/static/layui/layui.js"></script>
<script>

    layui.use(['form', 'layer', 'jquery', 'laypage'], function () {
        var form = layui.form();
        var layer = layui.layer;
        var laypage = layui.laypage;
        var $ = layui.jquery;

        var pindex = "${page.pageNum}";// 当前页
        var ptotalpages = "${page.pages}";// 总页数
        var pcount = "${page.total}";// 数据总数

        // 分页
        laypage({
            cont: 'form_page', // 页面上的id
            pages: ptotalpages,//总页数
            curr: pindex,//当前页。
            skip: true,
            jump: function (obj, first) {
                $("#currentPage").val(obj.curr);//设置当前页
                //防止无限刷新,
                //只有监听到的页面index 和当前页不一样是才出发分页查询
                if (obj.curr != pindex) {
                    console.log(pindex);
                    $("#pageSubmit").submit();
                }
            }
        });


//        商品分类和赠品的联动
        form.on("select(pPrdNo)", function (data) {
//            自定义的select标签的属性值
            var abc = $("#pPrdNo option:selected").attr("abc");
            console.log("abc = "+abc);
            console.log("data.value = "+data.value);
            $.ajax({
                url: "/slgf/getPrdDetailInfo?prd_name=" + abc,
                type: "POST",
                success: function (data) {
                    console.log(data.length);
                    $("#gPrdNo").find('option').remove();
                    for (var i = 0; i < data.length; i++) {
                        $("#gPrdNo").append("<option value='" + data[i].prd_dtl_id + "'>" + data[i].prd_dtl_name + "</option>");
                    }
                    // 重新渲染页面
                    form.render();
                }
            });

        });


        // 监听查询提交按钮
        form.on('submit(manageSLGF)', function (data) {

            $.ajax({
                url: "/slgf/selectSLGFInfo",  //后台程序地址
                type: "POST",
                data: data.field,  //需要post的数据
                success: function (data) {
                }
            });
        });

//        买赠详情弹出窗
        $(".detail").on("click", function () {
            var id = this.value;
            console.log("id = " + id);

            layer.open({
                title: '买赠详情 - 惠买企划系统',
                area: ['700px', '500px'],
                type: 2,
                content: '/slgf/selectSLGFDetailInfo?id=' + id
            });
        });

        var conNo = '${params.conNo!""}';
        var pPrdNo = '${params.pPrdNo!""}';
        var gPrdNo = '${params.gPrdNo!""}';

//         买赠促销方式动态赋值
        $("#conNo").find("option[value = '"+conNo+"']").attr("selected","selected");
//        主品类别动态赋值
        $("#pPrdNo").find("option[value = '"+pPrdNo+"']").attr("selected","selected");
//        赠品类别动态赋值
        if (gPrdNo.length>1) {
            alert("这是在判断里面");
            $.ajax({
                url: "/slgf/getPrdDetailInfo?prd_name=" +$("#pPrdNo option:selected").attr("abc"),
                type: "POST",
                success: function (data) {
                    console.log(data.length);
                    $("#gPrdNo").find('option').remove();
                    for (var i = 0; i < data.length; i++) {
                        $("#gPrdNo").append("<option value='" + data[i].prd_dtl_id + "'>" + data[i].prd_dtl_name + "</option>");
                    }
                    $("#gPrdNo").find("option[value = '"+gPrdNo+"']").attr("selected","selected");
                    // 重新渲染页面
                    form.render();
                }
            });
        }
        // 重新渲染页面
        form.render();
    });
</script>
</body>
</html>